package com.duoketech.controller.web.room;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.service.web.ModulePicService;
import com.duoketech.service.web.room.UnitRoomService;
import com.duoketech.service.web.unit.UnitService;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.upload.QiNiuUpload;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class UnitRoomController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",10);
        Kv kv = Kv.by(Constant.MEMBER_ID_KEY,getShopMemberId());
        Page<UnitRoom> result = (Page<UnitRoom>) UnitRoomService.ME.pageAllRecord(getModel(UnitRoom.class),pageNum,pageSize,kv);
        for (UnitRoom each:result.getList()) {
            each.set(UnitRoom.STATUS,each.status());
            each.set(UnitRoom.PRICE,each.price());
        }
        renderObject(result);
    }

    @Override
    public void add() {
        setAttr("unitList",UnitService.ME.queryUnitBy(getShopMemberId()));
        render("add-"+viewModel()+".html");
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        UnitRoom u = (UnitRoom) UnitRoomService.ME.queryById(id);
        u.set(UnitRoom.PRICE,PageUtil.fengToYuan(u.getInt(UnitRoom.PRICE)));
        setAttr("unitRoom",u);
        setAttr("unitList",UnitService.ME.queryUnitBy(getShopMemberId()));
        render("edit-"+viewModel()+".html");
    }

    @Override
    public void doAdd() {
        try {
            UnitRoom unitRoom = getModel(UnitRoom.class);
            String price = getPara("price","1.0");
            String[] equips = getParaValues("unitRoom.equip");
            String result = PageUtil.split(equips);
            unitRoom.set(UnitRoom.EQUIP,result);
            unitRoom.set(UnitRoom.CREATE_BY,getShopMemberId());
            unitRoom.set(UnitRoom.PRICE,PageUtil.yuanToFeng(Double.valueOf(price)));
            UnitRoomService.ME.doAdd(unitRoom,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            UnitRoom unitRoom = getModel(UnitRoom.class);
            String price = getPara("price","1.0");
            String[] equips = getParaValues("unitRoom.equip");
            String result = PageUtil.split(equips);
            unitRoom.set(UnitRoom.EQUIP,result);
            unitRoom.set(UnitRoom.PRICE,PageUtil.yuanToFeng(Double.valueOf(price)));
            unitRoom.set(UnitRoom.CREATE_BY,getShopMemberId());
            UnitRoomService.ME.update(unitRoom,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        UnitRoomService.ME.deleteBy(id,getModel(UnitRoom.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }
  }